unit R_DocumentosPorMateria;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, QRCtrls, QuickRpt, DB, ZAbstractRODataset, ZDataset, ExtCtrls;

type
  TFR_DocumentosPorMateria = class(TForm)
    Reporte: TQuickRep;
    QRBand1: TQRBand;
    QRImage2: TQRImage;
    QRLabel4: TQRLabel;
    QRImage1: TQRImage;
    QRLabel2: TQRLabel;
    TitleBand1: TQRBand;
    QRLabel1: TQRLabel;
    QRLabel3: TQRLabel;
    QRLabel7: TQRLabel;
    ZR_Consulta: TZReadOnlyQuery;
    QRGroup2: TQRGroup;
    QRGroup1: TQRGroup;
    QRDBText1: TQRDBText;
    QRPageFooter: TQRBand;
    QRLabel5: TQRLabel;
    QRLabel6: TQRLabel;
    QRSysData1: TQRSysData;
    QRExpr2: TQRExpr;
    QRLabel8: TQRLabel;
    QRExpr1: TQRExpr;
  private
    { Private declarations }
  public
    { Public declarations }
    constructor create(AOwner: TComponent; Documentos: String);
  end;

implementation

{$R *.dfm}

constructor TFR_DocumentosPorMateria.create(AOwner: TComponent; Documentos: String);
var
  s:string;
begin
  inherited create(AOwner);

  s:= 'SELECT D.ID, TITULO, COALESCE (DENOMINACION,  '+QuotedStr('Materia no disponible')+') AS MATERIA, COALESCE (CUENTA, 0) AS NRO_COPIAS '+
      ' FROM DOCUMENTOS_POR_MATERIA DMA '+
      ' JOIN DOCUMENTOS D ON DMA.FK_ID_DOCUMENTO = D.ID '+
      ' LEFT JOIN '+
      ' (SELECT FK_ID_DOCUMENTO, FK_ID_MATERIA AS IDMATERIA, SUM (NRO_COPIAS) AS CUENTA '+
      ' FROM DETALLE_PEDIDO '+
      ' WHERE FK_ID_MATERIA IN ('+Documentos+') '+
      ' GROUP BY FK_ID_DOCUMENTO, FK_ID_MATERIA) AS CNTA ON CNTA.FK_ID_DOCUMENTO = D.ID '+
      ' LEFT JOIN MATERIAS MAT ON MAT.ID = DMA.FK_ID_MATERIA '+
      ' WHERE DMA.FK_ID_MATERIA IN ('+Documentos+') '+
      ' ORDER BY MATERIA, NRO_COPIAS DESC, TITULO';

  with ZR_Consulta do
    begin
      SQL.Clear;
      Close;
      SQL.Add(s);
      Open;
    end;


end;

end.
